import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.pim.smart.hibernate.domain.ContactMain;
import org.pim.smart.hibernate.domain.ContactMainDAO;
import org.pim.smart.hibernate.domain.SysUser;
import org.pim.smart.hibernate.domain.SysUserDAO;


public class MyTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SysUserDAO userDao = new SysUserDAO();
		SysUser currentUser = userDao.findById(new Integer(1));
		ContactMainDAO contactDao = new ContactMainDAO();
		Session session = contactDao.getSession();
		Transaction tx = session.beginTransaction();
		StringBuffer sql = new StringBuffer();
		sql.append("select contact from ContactMain as contact where contact.contactMainOid in (");
		sql.append("select map.id.contactMainOid from ContactGroupMap as map where map.id.groupOid in (");
		sql.append("select group.groupOid from GroupContact as group where group.sysUser.userOid = " + currentUser.getUserOid().toString());
		sql.append(")) order by contact.createDate asc");
		Query query = session.createQuery(sql.toString());
		List result = query.list();
		Set<ContactMain> resultSet = new HashSet<ContactMain>(result);
		for (Iterator<ContactMain> it = result.iterator(); it.hasNext();){
			ContactMain contact = it.next();
			System.out.println(contact.getContactName() + ": Nick is "+contact.getContactNick()+", his/her oid is "+contact.getContactMainOid().toString());
		}
		System.out.print(resultSet.size());
		tx.commit();
		
		userDao.getSession().close();
		contactDao.getSession().close();
	}

}
